home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / tex / dvi / dvipssrc.zoo / squeeze.c < prev    next >
C/C++ Source or Header  |  1990-11-04  |  3KB  |  146 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  *   All Rights Reserved.
  4.  */
  5. /*
  6.  *   This routine squeezes a PostScript file down to its
  7.  *   minimum.  We parse and then output it.
  8.  */
  9. #include <stdio.h>
  10. #define LINELENGTH (78)
  11. #define BUFLENGTH (1000)
  12. #undef putchar
  13. #define putchar(a) (void)putc(a, out) ;
  14. FILE *in, *out ;
  15. static int linepos = 0 ;
  16. static int lastspecial = 1 ;
  17. extern int strlen() ;
  18. /*
  19.  *   This next routine writes out a `special' character.  In this case,
  20.  *   we simply put it out, since any special character terminates the
  21.  *   preceding token.
  22.  */
  23. void specialout(c)
  24. char c ;
  25. {
  26.    if (linepos + 1 > LINELENGTH) {
  27.       putchar('\n') ;
  28.       linepos = 0 ;
  29.    }
  30.    putchar(c) ;
  31.    linepos++ ;
  32.    lastspecial = 1 ;
  33. }
  34. void strout(s)
  35. char *s ;
  36. {
  37.    if (linepos + strlen(s) > LINELENGTH) {
  38.       putchar('\n') ;
  39.       linepos = 0 ;
  40.    }
  41.    linepos += strlen(s) ;
  42.    while (*s != 0)
  43.       putchar(*s++) ;
  44.    lastspecial = 1 ;
  45. }
  46. void cmdout(s)
  47. char *s ;
  48. {
  49.    int l ;
  50.  
  51.    l = strlen(s) ;
  52.    if (linepos + l + 1 > LINELENGTH) {
  53.       putchar('\n') ;
  54.       linepos = 0 ;
  55.       lastspecial = 1 ;
  56.    }
  57.    if (! lastspecial) {
  58.       putchar(' ') ;
  59.       linepos++ ;
  60.    }
  61.    while (*s != 0) {
  62.       putchar(*s++) ;
  63.    }
  64.    linepos += l ;
  65.    lastspecial = 0 ;
  66. }
  67. char buf[BUFLENGTH] ;
  68. #ifndef VMS
  69. void
  70. #endif
  71. main(argc, argv)
  72. int argc ;
  73. char *argv[] ;
  74. {
  75.    int c ;
  76.    char *b ;
  77.    char seeking ;
  78.    extern void exit() ;
  79.  
  80.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  81.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  82.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  83.       exit(1) ;
  84.    }
  85.    (void)fprintf(out, "%%!\n") ;
  86.    while (1) {
  87.       c = getc(in) ;
  88.       if (c==EOF)
  89.          break ;
  90.       if (c=='%') {
  91.          while ((c=getc(in))!='\n') ;
  92.       }
  93.       if (c <= ' ')
  94.          continue ;
  95.       switch (c) {
  96. case '{' :
  97. case '}' :
  98. case '[' :
  99. case ']' :
  100.          specialout(c) ;
  101.          break ;
  102. case '<' :
  103. case '(' :
  104.          if (c=='(')
  105.             seeking = ')' ;
  106.          else
  107.             seeking = '>' ;
  108.          b = buf ;
  109.          *b++ = c ;
  110.          do {
  111.             c = getc(in) ;
  112.             if (b > buf + BUFLENGTH-2) {
  113.                (void)fprintf(stderr, "Overran buffer seeking %n", seeking) ;
  114.                exit(1) ;
  115.             }
  116.             *b++ = c ;
  117.             if (c=='\\')
  118.                *b++ = getc(in) ;
  119.          } while (c != seeking) ;
  120.          *b++ = 0 ;
  121.          strout(buf) ;
  122.          break ;
  123. default:
  124.          b = buf ;
  125.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  126.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  127.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  128.                 (c=='=')||(c=='$')||(c=='+')) {
  129.             *b++ = c ;
  130.             c = getc(in) ;
  131.          }
  132.          if (b == buf) {
  133.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  134.             exit(1) ;
  135.          }
  136.          *b++ = 0 ;
  137.          (void)ungetc(c, in) ;
  138.          cmdout(buf) ;
  139.       }
  140.    }
  141.    if (linepos != 0)
  142.       putchar('\n') ;
  143.    exit(0) ;
  144.    /*NOTREACHED*/
  145. }
  146.